
#include "../libpatches.h"
#include "stereopan.h"
#include <math.h>

static void module_stereopan_processor(module m);

void module_stereopan_initializer(module m) {
	make_inputs(m, 2);
	make_outputs(m, 2);

	m->input_names[0] = "in";
	m->input_names[1] = "pos";
	m->output_names[0] = "left";
	m->output_names[1] = "right";

	m->processor = module_stereopan_processor;
}

static void module_stereopan_processor(module m) {

	sample s;
	output left = m->outputs[0];
	output right = m->outputs[1];
	input i = m->inputs[0];
	input p = m->inputs[1];

	/* 
	 * input range is -1 to 1
	 * convert to 0 to 1
	 * and error correct for out of range values
	 */

	s = **p * .5 + .5;
	s = (
		s > 1
		? 1
		: s < 0
		? 0
		: s
	);

	*right = **i * sqrt(s);
	*left = **i * sqrt(1 - s);
}

